<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/12">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/13. shell 练习-查找字母数小于6的单词/">shell 练习-查找字母数小于6的单词</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>bash for循环打印下面这句话中字母数不大于6的单词。</p>
<blockquote>
<p>Bash also interprets a number of multi-character options.</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">for</span> s <span class="keyword">in</span> Bash also interprets a number of multi-character options</div><div class="line"><span class="keyword">do</span></div><div class="line">    n=`<span class="built_in">echo</span> <span class="variable">$s</span>|wc-c`</div><div class="line">    <span class="keyword">if</span> [ ! <span class="variable">$n</span> -gt 6 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="variable">$s</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/12. shell 练习-删除文本中的字母/">shell 练习-删除文本中的字母</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>使用sed实现，把一个文本文档中的前5行中包含字母的行删除掉。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">head -5 1.txt|sed <span class="string">'/[a-Z]/d'</span></div></pre></td></tr></table></figure>
<p>把一个文本文档的前5行中包含字母的行删除掉，同时把6到10行中的全部字母删除掉。</p>
<p>假设文本名字叫做1.txt，并且文本行数大于10，脚本如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">##先获取该文本的行数</span></div><div class="line">nu=`wc -l 1.txt |awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line"><span class="comment">##对前5行进程处理</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 5`</div><div class="line"><span class="keyword">do</span> </div><div class="line">    <span class="comment">##使用sed把每一行的内容赋值给变量</span></div><div class="line">    l=`sed -n <span class="string">"<span class="variable">$i</span>"</span>p 1.txt`</div><div class="line">    <span class="comment">##用grep 判定是否匹配字母,-v取反，-q不输出内容</span></div><div class="line">    <span class="keyword">if</span> <span class="built_in">echo</span> <span class="variable">$l</span> |grep -vq <span class="string">'[a-zA-Z]'</span></div><div class="line">    <span class="keyword">then</span></div><div class="line">	<span class="built_in">echo</span> <span class="variable">$l</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="comment">##对6-10行做删除字母处理</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 6 10`</div><div class="line"><span class="keyword">do</span></div><div class="line">    l=`sed -n <span class="string">"<span class="variable">$i</span>"</span>p 1.txt`</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$l</span>|sed <span class="string">'s/[a-zA-Z]//g'</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="comment">##剩余的直接输出</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 11 <span class="variable">$nu</span>`</div><div class="line"><span class="keyword">do</span></div><div class="line">    sed -n <span class="string">"<span class="variable">$i</span>"</span>p 1.txt</div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="comment">##若想把更改内容写入到1.txt，还需要把以上内容重定向到一个文本中，然后删除1.txt，再把刚刚重定向的文件更名为1.txt</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/11. shell 练习-自动重启php-fpm/">shell 练习-自动重启php-fpm</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>服务器上跑的是LNMP环境，近期总是有502现象。502为网站访问的状态码，200正常，502错误是nginx最为普通的错误状态码。由于502只是暂时的，并且只要一重启php-fpm服务则502消失，但不重启的话，则会一直持续很长时间。所以有必要写一个监控脚本，监控访问日志的状态码，一旦发生502，则自动重启一下php-fpm。</p>
<p>我们设定： </p>
<ol>
<li>access_log  /data1/log/access.log</li>
<li>脚本死循环，每10s检测一次 （假设每10s钟的日志条数为300左右）</li>
<li>重启php-fpm的方法是  /etc/init.d/php-fpm restart</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#! /bin/bash</span></div><div class="line"><span class="built_in">log</span>=/data1/<span class="built_in">log</span>/access.log</div><div class="line">N=10</div><div class="line"><span class="keyword">while</span> :; <span class="keyword">do</span></div><div class="line">    <span class="comment">##因为10秒钟大概产生300条日志</span></div><div class="line">    tail -n 300 <span class="variable">$log</span> &gt; /tmp/<span class="built_in">log</span></div><div class="line">    n_502=`grep -c <span class="string">' 502"'</span> /tmp/<span class="built_in">log</span>`</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$n_502</span> -ge <span class="variable">$N</span> ]; <span class="keyword">then</span></div><div class="line">        <span class="comment">##记录系统的状态</span></div><div class="line">        top -bn1 &amp;gt;/tmp/`date +%H%M%S`-top.log</div><div class="line">        vmstat 1 5 &amp;gt;/tmp/`date +%H%M%S`-vm.log</div><div class="line">        /etc/init.d/php-fpm restart 2&amp;gt; /dev/null</div><div class="line">        <span class="comment">##重启php-fpm服务后，应先暂缓1分钟，而后继续每隔10s检测一次</span></div><div class="line">        sleep 60</div><div class="line">    <span class="keyword">fi</span></div><div class="line">    sleep 10</div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/10. shell 练习-每天删除两天前的文件/">shell 练习-每天删除两天前的文件</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>web服务器上有个目录，它的结构是这样的，首先有256个一级子目录和一个特殊目录，另外这256个一级子目录下还有256个二级子目录（特殊目录除外）。在这些二级子目录下有很多小文件，而且是每时每刻都会生成很多个。 虽然文件不大，但是时间长了，逐渐发现该分区的下inode快被占用满了。 所以请写一个小脚本，实现每天删除两天前的小文件，注意忽略那个特殊的一级子目录default。</p>
<p>目录： /data/web/abc/    </p>
<blockquote>
<p>三级子目录： 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de default df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff  （三级子目录下没有default目录）</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#! /bin/bash</span></div><div class="line"><span class="built_in">cd</span> /data/web/abc</div><div class="line"><span class="keyword">for</span> f <span class="keyword">in</span> `ls |grep -v default`; <span class="keyword">do</span></div><div class="line">    <span class="keyword">for</span> f1 <span class="keyword">in</span> `ls <span class="variable">$f</span>`; <span class="keyword">do</span></div><div class="line">        find <span class="variable">$f</span>/<span class="variable">$f1</span> -<span class="built_in">type</span> f -mtime +2|xargs rm -f</div><div class="line">    <span class="keyword">done</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/1. shell 练习-每日生成一个文件/">shell 练习-每日生成一个文件</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>请按照这样的日期格式（xxxx-xx-xx）每日生成一个文件，例如今天生成的文件为2013-09-23.log， 并且把磁盘的使用情况写到到这个文件中</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">d=`date +%F`</div><div class="line">logfile=<span class="variable">$d</span>.<span class="built_in">log</span></div><div class="line">df -h &gt; <span class="variable">$d</span>.<span class="built_in">log</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后使用 crontab -e 把脚本加到每日定点任务计划中即可</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/9. if 逻辑判断/">if 逻辑判断</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 shell 脚本中可以使用 if 逻辑判断，只不过它在 shell 中的语法有点奇怪。</p>
<h2 id="1-不带-else"><a href="#1-不带-else" class="headerlink" title="1.不带 else"></a>1.不带 else</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> 判断语句；<span class="keyword">then</span>  </div><div class="line">    <span class="built_in">command</span>  </div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim if1.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input your scor:"</span> a</div><div class="line"><span class="keyword">if</span>((a-lt60))</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"You didn' pass the exam."</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 if1.sh 中出现了 ((a -lt 60)) 这样的形式，这是 shell 脚本中特有的格式，他等于 if[$a-lt60] ，用一个小括号或者不用都会报错，需要记住这个格式。执行结果为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh if1.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/if%20%E9%80%BB%E8%BE%91%E5%88%A4%E6%96%AD/01.png?raw=true" alt=""></figure></p>
<h2 id="2-带有-else"><a href="#2-带有-else" class="headerlink" title="2.带有 else"></a>2.带有 else</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span>  判断语句；<span class="keyword">then</span> </div><div class="line">    <span class="built_in">command</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">command</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim if2.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input your score:"</span> a</div><div class="line"><span class="keyword">if</span>((a&lt;60))</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"You didn't pass the exam."</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"Good! You passed the exam."</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh if2.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/if%20%E9%80%BB%E8%BE%91%E5%88%A4%E6%96%AD/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;和上例唯一区别的地方是，如果输入大于 60 的数字会有提示。</p>
<h2 id="3-带有-elif"><a href="#3-带有-elif" class="headerlink" title="3.带有 elif"></a>3.带有 elif</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> 判断语句；<span class="keyword">then</span></div><div class="line">    <span class="built_in">command</span></div><div class="line"><span class="keyword">elif</span> 判断语句二；<span class="keyword">then</span></div><div class="line">    <span class="built_in">command</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">command</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim if3.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input your score:"</span> a</div><div class="line"><span class="keyword">if</span> ((a&lt;60))</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"You didn't pass the exam."</span></div><div class="line"><span class="keyword">elif</span> ((a&gt;=60))&amp;&amp;((a&lt;85))</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"Good! You pass the exam."</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"very good! Your score is very high!"</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的 &amp;&amp; 表示 “并且” 的意思，当然也可以使用 || 表示 “或者”</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行结果为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh if3.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/if%20%E9%80%BB%E8%BE%91%E5%88%A4%E6%96%AD/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在判断数值大小除了可以用 (()) 的形式外，还可以用 [] 但是就不能使用  &gt; 、&lt;、= 这样的符号了，要使用 -lt （小于），-gt（大于），-le（小于等于），-ge（大于等于），-eq（等于），-ne（不等于）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面以命令行的形式简单比较</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -lt 5 ];then echo ok;fi</span></div><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -gt 5 ];then echo ok;fi </span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -ge 10 ];then echo ok;fi </span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -eq 10 ];then echo ok;fi </span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -ne 10 ];then echo ok;fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再看看 if 中使用 &amp;&amp; 和 || 的情况：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -lt 1 ]||[ $a -gt 5 ];then echo ok;fi </span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -gt 1 ]||[ $a -lt 10 ];then echo ok;fi </span></div><div class="line">ok</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/8. shell 自定义变量/">shell 自定义变量</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 shell 脚本中使用变量显得脚本更加专业更像是一门语言，变量的作用当然不是为了专业。比如写了有一个长达1000行的 shell 脚本，并且脚本中出现了某一个命令或者路径几百次，如果突然发现不对，想换一下，那不是要更改几百次。当然可以用批量替换的命令，但是也很麻烦，并且脚本显得臃肿。变量的作用就是用来解决这个问题。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># cd /usr/local/sbin/</span></div><div class="line">[root@192 sbin]<span class="comment"># vim variable.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#In this script we will use variables.</span></div><div class="line">d=`date +%H:%M:%S`</div><div class="line"><span class="built_in">echo</span> <span class="string">"The script degin at <span class="variable">$d</span>."</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"Now we'll sleep 2 seconds."</span></div><div class="line">sleep 2</div><div class="line">d1=`date +%H:%M:%S`</div><div class="line"><span class="built_in">echo</span> <span class="string">"The script end at <span class="variable">$d1</span>."</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本中使用到了反引号，单引号的作用是执行命令。‘d’ 和 ‘d1’ 在脚本中作为变量出现。定义变量的格式为： 变量名=变量的值，当在脚本中引用变量时需要加上 ‘$’ 符号。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本执行结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh variable.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/01.png?raw=true" alt=""></figure></p>
<h2 id="数学运算"><a href="#数学运算" class="headerlink" title="数学运算"></a>数学运算</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 shell 中经常会用到数学运算，下面示例脚本用来计算两个数字的和。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim sum.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;写入内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#For get the sum of tow numbers.</span></div><div class="line"> </div><div class="line">a=1</div><div class="line">b=2</div><div class="line">sum=$[<span class="variable">$a</span>+<span class="variable">$b</span>]</div><div class="line"><span class="built_in">echo</span> <span class="string">"<span class="variable">$a</span>+<span class="variable">$b</span>=<span class="variable">$sum</span>"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>数学计算要用 [] 括起来并且外头要带一个 ‘$’ </strong> ,脚本结果为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh sum.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面为几个数学运算相关的例子：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;乘法运算：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh 1.sh</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">a=3</div><div class="line">b=2</div><div class="line">c=$[<span class="variable">$a</span>*<span class="variable">$b</span>]</div><div class="line"><span class="built_in">echo</span> <span class="variable">$c</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;除法运算：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim 2.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">a=10</div><div class="line">b=3</div><div class="line">c=$[<span class="variable">$a</span>/<span class="variable">$b</span>]</div><div class="line"><span class="built_in">echo</span> <span class="variable">$c</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/04.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;除法运算中 ，c 的结果为 3 ，并不是一个小数，这是因为 shell 默认是不支持小数的。如果想要看小数，需要借助于 bc ，bc 工具是 linux 系统里面的计算器，如果没有就先安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># yum install -y bc</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># echo "scale=2;10/3"|bc</span></div><div class="line">3.33</div></pre></td></tr></table></figure>
<h2 id="和用户交互"><a href="#和用户交互" class="headerlink" title="和用户交互"></a>和用户交互</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell 脚本可以实现，让用户输入一些字符串或者让用户去选择的行为。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim read.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#Using 'read' in shell script.</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input a number:"</span> x</div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input another number:"</span> y</div><div class="line">sum=$[<span class="variable">$x</span>+<span class="variable">$y</span>]</div><div class="line"><span class="built_in">echo</span> <span class="string">"The sum of the two numbers is:<span class="variable">$sum</span>"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;read 命令就是用在这样的地方，用于和用户交互。它把用户输入的字符串作为变量值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh read.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/05.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加上 -x 选项，再来看看这个执行过程：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh -x read.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/06.png?raw=true" alt=""></figure></p>
<h2 id="shell-脚本预设变量"><a href="#shell-脚本预设变量" class="headerlink" title="shell 脚本预设变量"></a>shell 脚本预设变量</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有时候会用到这样的命令 /etc/init.d/iptables restart 前面的 /etc/init.d/iptables 文件其实就是一个 shell 脚本，为什么后边可以跟一个 restart ，这里就涉及到了 shell 脚本的 预设变量。实际上， shell 脚本在执行的时候后边是可以跟参数的，而且还可以跟多个。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim option.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">sum=$[<span class="variable">$1</span>+<span class="variable">$2</span>]</div><div class="line"><span class="built_in">echo</span> <span class="string">"sum=<span class="variable">$sum</span>"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行结果为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh -x option.sh 1 2</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/07.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在脚本中，会觉得奇怪，哪里来的 $1 和 $2 。这就是 shell 脚本的预设变量，其中 $1 的值就是在执行的时候输入的 1 ，而 $2 的值就是在执行的时候输入的 $2 ，当然一个 shell 脚本的预设变量是没有限制的。另外，还有一个 $0  ，不过它代表的是脚本本身的名字。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改一下脚本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim option.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;写入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"<span class="variable">$1</span> <span class="variable">$2</span> <span class="variable">$0</span>"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh -x option.sh 1 2</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/08.png?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/7. shell脚本编程/">shell脚本编程</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<h2 id="1-shell脚本是什么"><a href="#1-shell脚本是什么" class="headerlink" title="1.shell脚本是什么"></a>1.shell脚本是什么</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;它是一种脚本语言，并非编程语言</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以使用一些逻辑判断、循环等语法</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以自定义子函数</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;是系统命令的集合</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell脚本可以实现自动化运维，大大增加我们的工作效率</p>
<h2 id="2-shell脚本结构以及执行方法"><a href="#2-shell脚本结构以及执行方法" class="headerlink" title="2.shell脚本结构以及执行方法"></a>2.shell脚本结构以及执行方法</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;开头行指定bash路径: #! /bin/bash</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以#开头的行作为解释说明</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本的名字以.sh结尾，用于区分这是一个shell脚本</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行方式有两种：chmod +x 1.sh; ./1.sh 如果没有执行权限可以 bash 1.sh </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;bash -x 1.sh 可以查看脚本执行过程</p>
<h2 id="3-学会date命令的用法"><a href="#3-学会date命令的用法" class="headerlink" title="3.学会date命令的用法"></a>3.学会date命令的用法</h2><ul>
<li>date  +%Y-%m-%d, date +%y-%m-%d 年月日</li>
<li>date  +%H:%M:%S = date +%T 时间</li>
<li>date +%s  时间戳</li>
<li>date -d @1434248742</li>
<li>date -d “+1day”一天后date -d “-1day”一天前</li>
<li>date -d  “-1month” 一月前</li>
<li>date -d  “-1min” 一分钟前</li>
<li>date +%w, date +%W 星期</li>
</ul>
<h2 id="4-shell脚本中的变量"><a href="#4-shell脚本中的变量" class="headerlink" title="4.shell脚本中的变量"></a>4.shell脚本中的变量</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用条件语句时，常常使用变量    if [ $a -gt 1 ]; then … ; fi</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;引用某个命令的结果时，用变量替代   n=<code>wc -l 1.txt</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;写和用户交互的脚本时，变量也是必不可少的  read -p “Input a number: “ n; echo $n   如果没写这个n，可以直接使用$REPLY</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内置变量 $0, $1, $2…    $0表示脚本本身，$1 第一个参数，$2 第二个 ….       $#表示参数个数</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数学运算a=1;b=2; c=$(($a+$b))或者$[$a+$b]</p>
<h2 id="5-shell中的逻辑判断"><a href="#5-shell中的逻辑判断" class="headerlink" title="5.shell中的逻辑判断"></a>5.shell中的逻辑判断</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式1：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> 条件 ; <span class="keyword">then</span> 语句; <span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式2：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> 条件; <span class="keyword">then</span> 语句; <span class="keyword">else</span> 语句; <span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式3：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> …; <span class="keyword">then</span> … ;<span class="keyword">elif</span> …; <span class="keyword">then</span> …; <span class="keyword">else</span> …; <span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;逻辑判断表达式：if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (&gt;); -lt(&lt;); -ge(&gt;=); -le(&lt;=);-eq(==); -ne(!=) 注意到处都是空格</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以使用 &amp;&amp; || 结合多个条件</p>
<h2 id="6-if-判断文件、目录属性"><a href="#6-if-判断文件、目录属性" class="headerlink" title="6.if 判断文件、目录属性"></a>6.if 判断文件、目录属性</h2><ul>
<li>[ -f file ]判断是否是普通文件，且存在 </li>
<li>[ -d file ] 判断是否是目录，且存在</li>
<li>[ -e file ] 判断文件或目录是否存在</li>
<li>[ -r file ] 判断文件是否可读</li>
<li>[ -w file ] 判断文件是否可写</li>
<li>[ -x file ] 判断文件是否可执行</li>
<li>[ -s file ] 判断文件大小是否非0</li>
<li>[ -c file ] 判断文件是否为字符特殊文件，且存在</li>
<li>[ -b file ] 判断文件是否为块特殊文件，且存在</li>
<li>[ -t file ] 判断文件是否为描述符（默认为1）指定的设备为终端</li>
<li>[ -L file ] 判断制定的是否为符号链接</li>
<li>[ “$a” = “$b” ] 判断 $a 和 $b 是否相等</li>
<li>[ -n “$var” ] 判断 $var 变量是否有值</li>
<li>[ -z  ] 判断变量是否存在值</li>
<li>[!    ] 测试条件的否定符号</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">[ -a FILE ] 如果 FILE 存在则为真。 </div><div class="line">[ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真。 </div><div class="line">[ -c FILE ] 如果 FILE 存在且是一个字特殊文件则为真。 </div><div class="line">[ -d FILE ] 如果 FILE 存在且是一个目录则为真。 </div><div class="line">[ -e FILE ] 如果 FILE 存在则为真。 </div><div class="line">[ -f FILE ] 如果 FILE 存在且是一个普通文件则为真。 </div><div class="line">[ -g FILE ] 如果 FILE 存在且已经设置了SGID则为真。 </div><div class="line">[ -h FILE ] 如果 FILE 存在且是一个符号连接则为真。 </div><div class="line">[ -k FILE ] 如果 FILE 存在且已经设置了粘制位则为真。 </div><div class="line">[ -p FILE ] 如果 FILE 存在且是一个名字管道(F如果O)则为真。 </div><div class="line">[ -r FILE ] 如果 FILE 存在且是可读的则为真。 </div><div class="line">[ -s FILE ] 如果 FILE 存在且大小不为0则为真。 </div><div class="line">[ -t FD ] 如果文件描述符 FD 打开且指向一个终端则为真。 </div><div class="line">[ -u FILE ] 如果 FILE 存在且设置了SUID (<span class="built_in">set</span> user ID)则为真。 </div><div class="line">[ -w FILE ] 如果 FILE 如果 FILE 存在且是可写的则为真。 </div><div class="line">[ -x FILE ] 如果 FILE 存在且是可执行的则为真。 </div><div class="line">[ -O FILE ] 如果 FILE 存在且属有效用户ID则为真。 </div><div class="line">[ -G FILE ] 如果 FILE 存在且属有效用户组则为真。 </div><div class="line">[ -L FILE ] 如果 FILE 存在且是一个符号连接则为真。 </div><div class="line">[ -N FILE ] 如果 FILE 存在 and has been mod如果ied since it was last <span class="built_in">read</span>则为真。 </div><div class="line">[ -S FILE ] 如果 FILE 存在且是一个套接字则为真。 </div><div class="line">[ FILE1 -nt FILE2 ] 如果 FILE1 has been changed more recently than FILE2, or 如果 FILE1 exists and FILE2 does not则为真。 </div><div class="line">[ FILE1 -ot FILE2 ] 如果 FILE1 比 FILE2 要老, 或者 FILE2 存在且 FILE1 不存在则为真。 </div><div class="line">[ FILE1 -ef FILE2 ] 如果 FILE1 和 FILE2 指向相同的设备和节点号则为真。 </div><div class="line">[ -o OPTIONNAME ] 如果 shell选项 “OPTIONNAME” 开启则为真。 </div><div class="line">[ -z STRING ] “STRING” 的长度为零则为真。 </div><div class="line">[ -n STRING ] or [ STRING ] “STRING” 的长度为非零 non-zero则为真。 </div><div class="line">[ STRING1 == STRING2 ] 如果2个字符串相同。 “=” may be used instead of “==” <span class="keyword">for</span> strict POSIX compliance则为真。 </div><div class="line">[ STRING1 != STRING2 ] 如果字符串不相等则为真。 </div><div class="line">[ STRING1 &lt; STRING2 ] 如果 “STRING1” sorts before “STRING2” lexicographically <span class="keyword">in</span> the current locale则为真。 </div><div class="line">[ STRING1 &gt; STRING2 ] 如果 “STRING1” sorts after “STRING2” lexicographically <span class="keyword">in</span> the current locale则为真。 </div><div class="line">[ ARG1 OP ARG2 ] “OP” is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators <span class="built_in">return</span> <span class="literal">true</span> <span class="keyword">if</span> “ARG1” is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers.</div></pre></td></tr></table></figure>
<h2 id="7-if-判断一些特殊用法"><a href="#7-if-判断一些特殊用法" class="headerlink" title="7.if 判断一些特殊用法"></a>7.if 判断一些特殊用法</h2><ul>
<li>if [ -z $a ]  这个表示当变量a的值为空时会怎么样</li>
<li>if grep -q ‘123’ 1.txt; then  表示如果1.txt中含有’123’的行时会怎么样</li>
<li>if [ ! -e file ]; then 表示文件不存在时会怎么样</li>
<li>if (($a<1)); 1="" then="" …等同于="" if="" [="" $a="" -lt="" ];="" then…="" ]="" 中不能使用<,="">,==,!=,&gt;=,&lt;=这样的符号</1));></li>
</ul>
<h2 id="8-shell中的case判断"><a href="#8-shell中的case判断" class="headerlink" title="8.shell中的case判断"></a>8.shell中的case判断</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">case</span>  变量名 <span class="keyword">in</span> </div><div class="line">    value1)</div><div class="line">        <span class="built_in">command</span></div><div class="line">        ;;</div><div class="line">    value2)</div><div class="line">        <span class="built_in">command</span></div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        commond</div><div class="line">        ;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在case程序中，可以在条件中使用|，表示或的意思， 比如    </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">2|3) </div><div class="line">    <span class="built_in">command</span></div><div class="line">    ;;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当变量为2或者3时，执行该部分命令。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;案例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input a number: "</span> n</div><div class="line"><span class="keyword">if</span> [ -z <span class="variable">$n</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"Please input a number."</span></div><div class="line">    <span class="built_in">exit</span> 1</div><div class="line"><span class="keyword">fi</span></div><div class="line">n1=`<span class="built_in">echo</span> <span class="variable">$n</span>|sed <span class="string">'s/[-0-9]//g'</span>`</div><div class="line"><span class="keyword">if</span> [ ! -z <span class="variable">$n1</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"Please input a number."</span></div><div class="line">    <span class="built_in">exit</span> 1</div><div class="line"><span class="comment">#elif [ $n -lt 0 ] || [ $n -gt 100 ]</span></div><div class="line"><span class="comment">#then</span></div><div class="line"><span class="comment">#    echo "The number range is 0-100."</span></div><div class="line"><span class="comment">#    exit 1</span></div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="keyword">if</span> [ <span class="variable">$n</span> -lt 60 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    tag=1</div><div class="line"><span class="keyword">elif</span> [ <span class="variable">$n</span> -ge 60 ] &amp;amp;&amp;amp; [ <span class="variable">$n</span> -lt 80 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    tag=2</div><div class="line"><span class="keyword">elif</span> [ <span class="variable">$n</span> -ge 80 ] &amp;amp;&amp;amp; [ <span class="variable">$n</span> -lt 90 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    tag=3</div><div class="line"><span class="keyword">elif</span> [ <span class="variable">$n</span> -ge 90 ] &amp;amp;&amp;amp; [ <span class="variable">$n</span> -le 100 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    tag=4</div><div class="line"><span class="keyword">else</span></div><div class="line">    tag=0</div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="keyword">case</span> <span class="variable">$tag</span> <span class="keyword">in</span></div><div class="line">    1)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"不及格"</span></div><div class="line">        ;;</div><div class="line">    2)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"及格"</span></div><div class="line">        ;;</div><div class="line">    3|4)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"优秀"</span></div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"The number range is 0-100."</span></div><div class="line">        ;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<h2 id="9-shell脚本中的循环"><a href="#9-shell脚本中的循环" class="headerlink" title="9.shell脚本中的循环"></a>9.shell脚本中的循环</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for循环语法结构: </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">for</span>  变量名 <span class="keyword">in</span> 条件; <span class="keyword">do</span> … <span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;案例1：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">sum=0</div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 100`</div><div class="line"><span class="keyword">do</span></div><div class="line">    sum=$[<span class="variable">$sum</span>+<span class="variable">$i</span>]</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> <span class="variable">$sum</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;案例2：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">cd</span> /etc/</div><div class="line"><span class="keyword">for</span> a <span class="keyword">in</span> `ls /etc/`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">if</span> [ -d <span class="variable">$a</span> ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        ls -d <span class="variable">$a</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;案例3：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">n=`wc -l 1.txt |awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 <span class="variable">$n</span>`</div><div class="line"><span class="keyword">do</span></div><div class="line">    sed -n <span class="string">"<span class="variable">$i</span>"</span>p 1.txt</div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;while 循环语法结构: </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">while</span> 条件; <span class="keyword">do</span> … <span class="keyword">done</span> 死循环用:表示</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;案例1：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">while</span> :</div><div class="line"><span class="keyword">do</span></div><div class="line">    load=`w|head -1|awk -F <span class="string">'load average: '</span> <span class="string">'&#123;print $2&#125;'</span>|cut -d. -f1`</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$load</span> -gt 10 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        top|mail -s <span class="string">"load is high: <span class="variable">$load</span>"</span> [email]asldkfls@11.com[/email]</div><div class="line">    <span class="keyword">fi</span></div><div class="line">    sleep 30</div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;案例2：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">while</span> :</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="built_in">read</span> -p <span class="string">"Please input a number: "</span> n</div><div class="line">    <span class="keyword">if</span> [ -z <span class="variable">$n</span> ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"你需要输入东西"</span></div><div class="line">        <span class="built_in">continue</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">    n1=`<span class="built_in">echo</span> <span class="variable">$n</span>|sed <span class="string">'s/[-0-9]//g'</span>`</div><div class="line">    <span class="keyword">if</span> [ ! -z <span class="variable">$n1</span> ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"你只能输入一个纯数字"</span></div><div class="line">        <span class="built_in">continue</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">    <span class="built_in">break</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> <span class="variable">$n</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;break直接结束本层循环; </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 5`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$i</span> == 3 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">break</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> aaaaaaa</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;continue忽略continue之下的代码，直接进行下一次循环 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 5`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$i</span> == 3 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">continue</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> <span class="variable">$i</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;exit 直接退出shell</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 5`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$i</span> == 3 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">exit</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> aaaaaaa</div></pre></td></tr></table></figure>
<h2 id="10-shell中的函数"><a href="#10-shell中的函数" class="headerlink" title="10.shell中的函数"></a>10.shell中的函数</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;函数就是把一段代码整理到了一个小单元中，并给这个小单元起一个名字，当用到这段代码时直接调用这个小单元的名字即可。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式: </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">function</span> <span class="function"><span class="title">f_name</span></span>() &#123;</div><div class="line">    <span class="built_in">command</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;函数必须要放在最前面</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;案例1：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="function"><span class="title">input</span></span>() &#123;</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$1</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;案例2：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="function"><span class="title">sum</span></span>() &#123;</div><div class="line">    s=$[<span class="variable">$1</span>+<span class="variable">$2</span>]</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$s</span></div><div class="line">&#125;</div><div class="line">sum 1 2</div></pre></td></tr></table></figure>
<p>案例3：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="function"><span class="title">ip</span></span>() &#123;</div><div class="line">    ifconfig |grep -A1 <span class="string">"<span class="variable">$1</span> "</span> |tail -1 |awk <span class="string">'&#123;print $2&#125;'</span>|awk -F<span class="string">':'</span> <span class="string">'&#123;print $2&#125;'</span></div><div class="line">&#125;</div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input the eth name: "</span> e</div><div class="line">myip=`ip <span class="variable">$e</span>`</div><div class="line"><span class="built_in">echo</span> <span class="string">"<span class="variable">$e</span> address is <span class="variable">$myip</span>"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;函数里可以export 全局变量</p>
<h2 id="11-shell练习题"><a href="#11-shell练习题" class="headerlink" title="11.shell练习题"></a>11.shell练习题</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编写shell脚本，计算1-100的和；</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">sum=0</div><div class="line"><span class="keyword">for</span>((i=1;i&lt;=100;i++));<span class="keyword">do</span></div><div class="line">    sum=$((i+sum))</div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> <span class="variable">$sum</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编写shell脚本，要求输入一个数字，然后计算出从1到输入数字的和，要求，如果输入的数字小于1，则重新输入，直到输入正确的数字为止；</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Enter a num:"</span> num</div><div class="line">sum=0</div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> $(seq <span class="variable">$num</span>)</div><div class="line"><span class="keyword">do</span></div><div class="line">    sum=$((sum+i))</div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"Sum is <span class="variable">$sum</span>"</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">n=0</div><div class="line"><span class="keyword">while</span> [ <span class="variable">$i</span> -lt 1 ]</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="built_in">read</span> -p <span class="string">"Enter a num:"</span> i</div><div class="line"><span class="keyword">done</span></div><div class="line">sum=0</div><div class="line"><span class="keyword">for</span> n <span class="keyword">in</span> `seq 1 <span class="variable">$i</span>`</div><div class="line"><span class="keyword">do</span></div><div class="line">    sum=$[<span class="variable">$sum</span>+<span class="variable">$n</span>]</div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">echo</span> <span class="variable">$sum</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编写shell脚本，把/root/目录下的所有目录（只需要一级）拷贝到/tmp/目录下；</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">cd</span> /root</div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `ls`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">if</span> [ -d <span class="variable">$i</span> ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        cp -r <span class="variable">$i</span> /tmp/</div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编写shell脚本，批量建立用户user_00, user_01, … user_100并且所有用户同属于users组；</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">groupadd users</div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 0 9`</div><div class="line"><span class="keyword">do</span></div><div class="line">    useradd -g users user_0<span class="variable">$i</span></div><div class="line"><span class="keyword">done</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> j <span class="keyword">in</span> `seq 10 100`</div><div class="line"><span class="keyword">do</span></div><div class="line">    useradd -g user_<span class="variable">$j</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编写shell脚本，截取文件test.log中包含关键词 ‘abc’ 的行中的第一列（假设分隔符为 ”:” ），然后把截取的数字排序（假设第一列为数字），然后打印出重复次数超过10次的列；</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">awk -F<span class="string">':'</span> <span class="string">'$0~/abc/ &#123;print $1&#125;'</span> test.log &gt; /tmp/1.txt</div><div class="line">sort -n /tmp/1.txt |uniq -c | sort -n &gt; /tmp/2.txt</div><div class="line">awk <span class="string">'$1&gt;10 &#123;print $2&#125;'</span> /tmp/2.txt</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/6. date 命令/">date命令</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;date 在脚本中用得很多，那必须得学会这个命令，下面是 date 的几种用法：</p>
<ol>
<li>输出四位的年、月、日，格式 2017.04.27</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># date +%Y-%m-%d</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/date%20%E5%91%BD%E4%BB%A4/01.png?raw=true" alt=""></figure></p>
<ol>
<li>输出两位的年、月、日，格式17-04-27</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># date +%y-%m-%d</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/date%20%E5%91%BD%E4%BB%A4/02.png?raw=true" alt=""></figure></p>
<ol>
<li>另外一种输出年、月、日的方式</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># date +%F</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/date%20%E5%91%BD%E4%BB%A4/03.png?raw=true" alt=""></figure></p>
<ol>
<li>输出时间，格式 02：05：31</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># date +%H:%M:%S</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/date%20%E5%91%BD%E4%BB%A4/04.png?raw=true" alt=""></figure></p>
<ol>
<li>另外一种输出时间的方式，格式同 4</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># date +%T</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/date%20%E5%91%BD%E4%BB%A4/05.png?raw=true" alt=""></figure></p>
<ol>
<li>时间戳</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># date +%s</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/date%20%E5%91%BD%E4%BB%A4/06.png?raw=true" alt=""></figure></p>
<ol>
<li>根据时间戳反向推测时间</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># date -d @1493232796</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/date%20%E5%91%BD%E4%BB%A4/07.png?raw=true" alt=""></figure></p>
<ol>
<li>一天后</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># date -d "+1day"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/date%20%E5%91%BD%E4%BB%A4/08.png?raw=true" alt=""></figure></p>
<ol>
<li>一天前</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># date -d "-1day"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/date%20%E5%91%BD%E4%BB%A4/09.png?raw=true" alt=""></figure></p>
<ol>
<li>一月前</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># date -d "-1month"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/date%20%E5%91%BD%E4%BB%A4/10.png?raw=true" alt=""></figure></p>
<ol>
<li>一分钟前</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># date -d "-1min"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/date%20%E5%91%BD%E4%BB%A4/11.png?raw=true" alt=""></figure></p>
<ol>
<li>星期</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;小写 w 表示周几</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># date +%w</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/date%20%E5%91%BD%E4%BB%A4/12.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;大写 W 表示本年的第几周</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># date +%W</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/date%20%E5%91%BD%E4%BB%A4/13.png?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/5. shell 备份 mysql/">shell 备份 mysql</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<ol>
<li>创建备份目录和文件</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mkdir/srv/bakmysql</div><div class="line">mkdir/srv/bakmysql/daily</div><div class="line">touch/srv/bakmysql/mysqlbak.log</div></pre></td></tr></table></figure>
<ol>
<li>启用二进制日志</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> /etc/</div><div class="line">vi my.cnf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在[mysqld]添加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">log</span>-bin=/var/lib/mysql/mysql-bin.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后重启mysqld服务器</p>
<ol>
<li>设置crontab任务，每天执行备份脚本</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> /etc/</div><div class="line">vi crontab</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加如下脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">0 0 * * 0 root/usr/sbin/mysqlfullbak     <span class="comment">#（每天0点执行完全备份脚本）</span></div><div class="line">0 1 * * * root/usr/sbin/mysqldailybak    <span class="comment"># (每天凌晨1点执行增量备份脚本)</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本内容：</p>
<ol>
<li>完全备份脚本</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment"># Name:mysqlfullbak</span></div><div class="line"><span class="comment"># This is mysql mysqlfullbak scripts</span></div><div class="line"><span class="comment"># By Cloudsoar</span></div><div class="line"><span class="comment"># Last Modify:2012-05-10</span></div><div class="line"><span class="comment">#定义脚本存放路径</span></div><div class="line">scriptsDir=/usr/sbin</div><div class="line"><span class="comment">#定义用户名及密码</span></div><div class="line">user=root</div><div class="line">userPWD=9dspod8a</div><div class="line"><span class="comment">#定义要备份的数据库</span></div><div class="line">database=GoToMyCloudDB</div><div class="line"><span class="comment">#定义完全备份文件存放路径</span></div><div class="line">bakDir=/srv/bakmysql</div><div class="line">eMailFile=<span class="variable">$bakDir</span>/email.txt</div><div class="line">eMail=xia.yan@cloudsoar.com</div><div class="line"><span class="comment">#定义日志文件</span></div><div class="line">LogFile=<span class="variable">$bakDir</span>/mysqlbak.log</div><div class="line">DATE=`date +%Y%m%d`</div><div class="line"><span class="built_in">echo</span> <span class="string">" "</span> &gt;&gt; <span class="variable">$LogFile</span></div><div class="line"><span class="built_in">echo</span> <span class="string">" "</span> &gt;&gt; <span class="variable">$LogFile</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"--------------------------"</span> &gt;&gt; <span class="variable">$LogFile</span></div><div class="line"><span class="built_in">echo</span> $(date +<span class="string">"%y-%m-%d %H:%M:%S"</span>) &gt;&gt;<span class="variable">$LogFile</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"-----------------"</span> &gt;&gt; <span class="variable">$LogFile</span></div><div class="line"><span class="built_in">cd</span> <span class="variable">$bakDir</span></div><div class="line">DumpFile=<span class="variable">$DATE</span>.sql</div><div class="line">mysqldump --flush-logs -u<span class="variable">$user</span> -p<span class="variable">$userPWD</span> --quick <span class="variable">$database</span> &gt;<span class="variable">$DumpFile</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"Dump Done"</span> &gt;&gt; <span class="variable">$LogFile</span></div><div class="line"><span class="built_in">echo</span>  <span class="string">"[<span class="variable">$DumpFile</span>]Backup Success!"</span> &gt;&gt; <span class="variable">$LogFile</span></div><div class="line">daily_databakDir=<span class="variable">$bakDir</span>/daily</div><div class="line"><span class="built_in">cd</span> <span class="variable">$bakDir</span>/daily</div><div class="line">find <span class="variable">$daily_databakDir</span> -name <span class="string">"daily*"</span> -<span class="built_in">type</span> f -mtime +35 -<span class="built_in">exec</span> rm &#123;&#125; \; &gt; /dev/null 2&gt;&amp;1</div></pre></td></tr></table></figure>
<ol>
<li>增量备份脚本</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment"># Name:mysqldailybak</span></div><div class="line"><span class="comment"># This ia mysql mysqldailybak scripts</span></div><div class="line"><span class="comment"># By Cloudsoar</span></div><div class="line"><span class="comment"># Last modify:2012-05-10</span></div><div class="line"><span class="comment">#定义用户名及密码</span></div><div class="line">user=root</div><div class="line">userPWD=9dspod8a</div><div class="line"><span class="comment">#定义数据库</span></div><div class="line">database=GotoMyCloudDB</div><div class="line">/usr/bin/mysqladmin -u<span class="variable">$user</span> -p<span class="variable">$userPWD</span> flush-logs</div><div class="line">daily_databakDir=/srv/bakmysql/daily</div><div class="line"><span class="comment">#定义MYSQL数据目录</span></div><div class="line">mysqlDataDir=/srv/mysql</div><div class="line">eMailFile=<span class="variable">$daily_databakDir</span>/email.txt</div><div class="line">eMail=xia.yan@cloudsoar.com</div><div class="line">DATE=`date +%Y%m%d`</div><div class="line">logFile=<span class="variable">$daily_databakDir</span>/mysql<span class="variable">$DATE</span>.<span class="built_in">log</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"       "</span> &gt; <span class="variable">$eMailFile</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"-----------------------"</span> &gt;&gt; <span class="variable">$eMailFile</span> </div><div class="line"><span class="built_in">echo</span> $(date +<span class="string">"%y-%m-%d %H:%M:%S"</span>) &gt;&gt; <span class="variable">$eMailFile</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"-------</span></div><div class="line"><span class="string">------------------"</span> &gt;&gt; <span class="variable">$eMailFile</span></div><div class="line">TIME=$(date <span class="string">"-d 10 day ago"</span> +%Y%m%d%H%M%S)</div><div class="line">StartTime=$(date <span class="string">"-d 1 day ago"</span> +<span class="string">"%Y-%m-%d %H:%M:%S"</span>)</div><div class="line"><span class="built_in">echo</span> “Delete 10 days before the <span class="built_in">log</span>” &gt;&gt;<span class="variable">$eMailFile</span></div><div class="line">mysql -u<span class="variable">$user</span> -p<span class="variable">$userPWD</span> -e <span class="string">"purge master logs before <span class="variable">$&#123;TIME&#125;</span>"</span> &amp;&amp; <span class="built_in">echo</span> <span class="string">"delete 10 days before log"</span> |tee -a <span class="variable">$eMailFile</span> <span class="comment">#删除10天前的2进制文件</span></div><div class="line">filename=`cat <span class="variable">$mysqlDataDir</span>/mysqld-bin.index |awk -F <span class="string">"/"</span> <span class="string">'&#123;print $2&#125;'</span>` <span class="comment"># 2进制文件</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="variable">$filename</span></div><div class="line"><span class="keyword">do</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"<span class="variable">$StartTime</span> start backup binlog"</span> &gt;&gt; <span class="variable">$eMailFile</span></div><div class="line">mysqlbinlog -u<span class="variable">$user</span> -p<span class="variable">$userPWD</span> -d <span class="variable">$database</span> --start-datetime=<span class="string">"<span class="variable">$StartTime</span>"</span> <span class="variable">$mysqlDataDir</span>/<span class="variable">$i</span> &gt;&gt; <span class="variable">$daily_databakDir</span>/daily<span class="variable">$DATE</span>.sql |tee -a <span class="variable">$eMailFile</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="keyword">if</span> [ $? = 0 ]</div><div class="line"><span class="keyword">then</span></div><div class="line"><span class="comment"># 删除mtime&gt;32的增量日志备份文件</span></div><div class="line">find <span class="variable">$daily_databakDir</span> -name <span class="string">"*.log"</span> -<span class="built_in">type</span> f -mtime +32 -<span class="built_in">exec</span> rm &#123;&#125; \; &gt; /dev/null 2&gt;&amp;1</div><div class="line"><span class="built_in">cd</span> <span class="variable">$daily_databakDir</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"Daily backup succeed"</span> &gt;&gt; <span class="variable">$eMailFile</span></div><div class="line"><span class="keyword">else</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"Daily backup fail"</span> &gt;&gt; <span class="variable">$eMailFile</span></div><div class="line">mail -s <span class="string">"MySQL Backup"</span> <span class="variable">$eMail</span> &lt; <span class="variable">$eMailFile</span> <span class="comment">#备份失败之后发送邮件通知</span></div><div class="line"><span class="keyword">fi</span></div><div class="line">cat <span class="variable">$eMailFile</span> &gt; <span class="variable">$logFile</span></div><div class="line"><span class="comment"># 删除mtime&gt;32的增量日志备份文件</span></div><div class="line">find <span class="variable">$daily_databakDir</span> -name <span class="string">"*.log"</span> -<span class="built_in">type</span> f -mtime +32 -<span class="built_in">exec</span> rm &#123;&#125; \; &gt; /dev/null 2&gt;&amp;</div></pre></td></tr></table></figure>
<ol>
<li>删除完全备份脚本</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/sh</span></div><div class="line"><span class="comment"># Name:rmBackup</span></div><div class="line"><span class="comment"># PS:Delete old Backup.</span></div><div class="line"><span class="comment"># By:Cloudsoar</span></div><div class="line"><span class="comment"># Last Modify:2012-05-10</span></div><div class="line"><span class="comment"># 定义备份目录</span></div><div class="line">dataBackupDir=/srv/bakmysql</div><div class="line"><span class="comment"># 删除mtime&gt;32的日志备份文件</span></div><div class="line">find <span class="variable">$dataBackupDir</span> -name <span class="string">"*.sql"</span> -<span class="built_in">type</span> f -mtime +32 -<span class="built_in">exec</span> rm &#123;&#125; \; &gt; /dev/null 2&gt;&amp;1</div></pre></td></tr></table></figure>
<ol>
<li>添加可执行权限</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">chmod +x mysqlfullbak</div><div class="line">chmod +x mysqldailybak</div><div class="line">chmod +x rmBackup</div></pre></td></tr></table></figure>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/14">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
